﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ModelLibrary;
using System.Text;

namespace WebC
{
    public partial class buy_project : clsBaseForm
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Ajax.Utility.RegisterTypeForAjax(typeof(buy_project));
            if (!IsPostBack)
            {
                loadInfo();
            }
        }

        #region 载入信息

        public void loadInfo()
        {
            BuyDevice buy = null;
            string sModelName = "";
            DataSet ds = null;
            try
            {
                buy = new BuyDevice();
                if (m_Id == 0)
                {
                    if (!checkModelRightById(213, ref sModelName, (clsBaseLib)buy))
                    {
                        errorUrl(string.Format("你没有“{0}”的权限,请与管理员联系！", sModelName));
                        return;
                    }
                    DataTable dt = buy.getUserDept(iUserId);
                    txtDeptId.Text = Convert.ToString(dt.Rows[0]["deptid"]);
                    txtDeptName.Text = Convert.ToString(dt.Rows[0]["deptname"]);
                }
                else
                {
                    if (Convert.ToInt32(Request["noedit"]) == 1)
                    {
                        if (!checkModelRightById(212, ref sModelName, (clsBaseLib)buy))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                    }
                    else
                    {
                        if (!checkModelRightById(215, ref sModelName, (clsBaseLib)buy))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                    }
                    ds = buy.getProjectInfoById(m_Id);
                }
                sModelFullName = sModelName;

            }
            catch (Exception e)
            {
                errorUrl(Convert.ToString(e));
            }
            finally
            {
                if (buy != null)
                {
                    buy.Dispose();
                }
            }
            if (m_Id > 0)
            {
                if (ds != null)
                {
                    DataTable dt = ds.Tables[0];
                    if (dt.Rows.Count == 1)
                    {
                        txtDeptId.Text = Convert.ToString(dt.Rows[0]["deptid"]);
                        txtDeptName.Text = Convert.ToString(dt.Rows[0]["deptname"]);
                        txtMemo.Text = Convert.ToString(dt.Rows[0]["memo"]);
                        txtProjectName.Text = Convert.ToString(dt.Rows[0]["projectname"]);
                        lbCreateDate.Text = Convert.ToDateTime(dt.Rows[0]["createdate"]).ToString("yyyy-MM-dd");
                        lbCreater.Text = Convert.ToString(dt.Rows[0]["username"]);
                    } 
                    dt = ds.Tables[1];  
                    int iRowCount = dt.Rows.Count;
                    StringBuilder sbu = new StringBuilder();                  
                    string sAppIds = "0";
                    for (int i = 0; i < iRowCount; i++)
                    {
                        sAppIds += string.Format(",{0}",dt.Rows[i]["appid"]);
                        sbu.Append(string.Format(@"<tr>
                                            <td style=""width:100px"">{0}</td>
                                            <td style=""width:100px"">{1}</td>
                                            <td style=""width:180px"">{2}</td>
                                        </tr>", dt.Rows[i]["appcode"], dt.Rows[i]["username"], dt.Rows[i]["allowdate"]));
                    }
                    hdapp.Value = sAppIds;
                    ltrApp.Text = Convert.ToString(sbu);
                    sbu = new StringBuilder();
                    dt = ds.Tables[2];
                    iRowCount = dt.Rows.Count;
                    for (int i = 0; i < iRowCount; i++)
                    {
                        sbu.Append(string.Format(@"<tr>
                        <td align=""left"" width=""120"">
                            <a href=""javascript:"" onclick=""insertdevice(this.parentNode.parentNode)"">+</a> <a href=""javascript:"" onclick=""removedevice(this.parentNode.parentNode)"">-</a><input style=""width: 92px"" type=""text"" onclick=""getdevicetype(this)"" value=""{0}"" /><input style=""width: 64px"" type=""hidden"" value=""{1}"" /></td>
                        <td width=""70"">
                            <input  style=""width: 70px"" type=""text"" value=""{2}"" /></td>
                        <td width=""70"">
                            <input  style=""width: 70px"" type=""text"" value=""{3}"" /></td>
                        <td><select style=""width: 188px"">
                                <option value=""{4}"">{5}</option>
                            </select></td>
                    </tr>", dt.Rows[i]["typename"], dt.Rows[i]["typeid"], dt.Rows[i]["devicenum"], dt.Rows[i]["singleprice"], dt.Rows[i]["brandid"], Convert.ToString(dt.Rows[i]["brandname"]) == "" ? "请选择品牌" : dt.Rows[i]["brandname"]));
                    }
                    ltrDevice.Text = Convert.ToString(sbu);
                }
            }
            else
            {
                lbCreateDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                lbCreater.Text = sUserName;
            }

        }

        #endregion

        #region 获得申请的设备

        [Ajax.AjaxMethod]
        public DataTable getAppDevice(string sIds)
        {
            BuyDevice buy = null;
            DataTable dt = null;
            try
            {
                buy = new BuyDevice();
                dt = buy.getSelectAppDeviceInfo(sIds);
            }
            catch
            {
                dt = null;
            }
            finally
            {
                if (buy != null)
                {
                    buy.Dispose();
                }
            }
            return dt;
        }        

        #endregion

        #region 获得产品品牌

        [Ajax.AjaxMethod]
        public string getBrandHtml(int iTypeId)
        {
            BuyDevice buy = null;
            DataTable dt = null;
            try
            {
                buy = new BuyDevice();
                dt = buy.getBrandByDeviceTypeId(iTypeId);
            }
            catch (Exception e)
            {
                return Convert.ToString(e);
            }
            finally
            {
                if (buy != null)
                {
                    buy.Dispose();
                }
            }
            StringBuilder sbu = new StringBuilder();
            sbu.Append(@"<select style=""width: 188px""><option selected=""selected"" value=""0"">请选择品牌</option>");
            int iRowCount = dt.Rows.Count;
            for (int i = 0; i < iRowCount; i++)
            {
                sbu.Append(string.Format(@"<option value=""{0}"">{1}</option>"
                    , dt.Rows[i]["brandid"], dt.Rows[i]["brandname"]));
            }
            sbu.Append(@"</select>");
            return Convert.ToString(sbu);
        }

        #endregion

        int m_Id
        {
            get
            {
                return Convert.ToInt32(Request["id"]);
            }
        }

        #region 保存信息

        [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
        public bool saveData(int iProjectId, string sProjectName, int iDept, string sApps, string sMemo, string[] sDevices)
        {
            BuyDevice buy = null;
            bool bResult = false;
            try
            {
                buy = new BuyDevice();
                sProjectName = sProjectName.Replace("'", "''");
                sMemo = sMemo.Replace("'", "''");
                for (int i = 0; i < sDevices.Length; i++)
                {
                    sDevices[i] = sDevices[i].Replace("'", "''");
                }
                bResult = buy.setProjectInfo(iProjectId, iUserId, sProjectName, iDept, sApps, sMemo, sDevices);
            }
            catch
            {
                bResult = false;
            }
            finally
            {
                if (buy != null)
                {
                    buy.Dispose();
                }
            }
            return bResult;
        }

        #endregion
    }
}